package com.itheima.service.impl;

import com.itheima.domain.SmsCode;
import com.itheima.service.SmsCodeService;
import com.itheima.utils.CodeUtils;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: SmsCodeServiceImpl
 * Description:
 * Date: 2023/03/11 AM 12:32:36
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private CacheChannel cacheChannel;

    /**
     * 发送验证码到手机
     *
     * @param tele {@link String}
     * @return {@link String}
     */
    @Override
    public String sendCodeToSms(String tele) {
        String code = codeUtils.generator(tele);
        cacheChannel.set("sms", tele, code);
        return code;

    }

    /**
     * 检查验证码
     *
     * @param smsCode {@link String}
     * @return {@link Boolean}
     */
    @Override
    public boolean checkCode(SmsCode smsCode) {
        String code = cacheChannel.get("sms", smsCode.getTele()).asString();
        return smsCode.getCode().equals(code);
    }

}
